package com.marchsoft.campushelp.service.impl;

import com.marchsoft.campushelp.domain.CampusUserTable;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import com.marchsoft.campushelp.repository.CampusUserTableRepository;
import com.marchsoft.campushelp.service.CampusUserTableService;
import com.marchsoft.campushelp.service.dto.CampusUserTableDto;
import com.marchsoft.campushelp.service.dto.CampusUserTableQueryCriteria;
import com.marchsoft.campushelp.service.mapper.CampusUserTableMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Service
//@CacheConfig(cacheNames = "campusUserTable")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CampusUserTableServiceImpl implements CampusUserTableService {

    private final CampusUserTableRepository campusUserTableRepository;

    private final CampusUserTableMapper campusUserTableMapper;

    public CampusUserTableServiceImpl(CampusUserTableRepository campusUserTableRepository, CampusUserTableMapper campusUserTableMapper) {
        this.campusUserTableRepository = campusUserTableRepository;
        this.campusUserTableMapper = campusUserTableMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CampusUserTableQueryCriteria criteria, Pageable pageable){
        Page<CampusUserTable> page = campusUserTableRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(campusUserTableMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CampusUserTableDto> queryAll(CampusUserTableQueryCriteria criteria){
        return campusUserTableMapper.toDto(campusUserTableRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CampusUserTableDto findById(Long id) {
        CampusUserTable campusUserTable = campusUserTableRepository.findById(id).orElseGet(CampusUserTable::new);
        ValidationUtil.isNull(campusUserTable.getId(),"CampusUserTable","id",id);
        return campusUserTableMapper.toDto(campusUserTable);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CampusUserTableDto create(CampusUserTable resources) {
        return campusUserTableMapper.toDto(campusUserTableRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CampusUserTable resources) {
        CampusUserTable campusUserTable = campusUserTableRepository.findById(resources.getId()).orElseGet(CampusUserTable::new);
        ValidationUtil.isNull( campusUserTable.getId(),"CampusUserTable","id",resources.getId());
        campusUserTable.copy(resources);
        campusUserTableRepository.save(campusUserTable);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            campusUserTableRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CampusUserTableDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CampusUserTableDto campusUserTable : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("昵称", campusUserTable.getNickName());
            map.put("头像", campusUserTable.getAvatar());
            map.put("VIOP等级", campusUserTable.getVipTyp());
            map.put("登入密码", campusUserTable.getLoginPassword());
            map.put("支付密码", campusUserTable.getPayPassword());
            map.put("名字", campusUserTable.getName());
            map.put("年龄", campusUserTable.getAge());
            map.put("电话", campusUserTable.getPhone());
            map.put("学校信息", campusUserTable.getCampus());
            map.put("1-老板，2-管理员，3-用户表（角色）", campusUserTable.getUserType());
            map.put("1-启动，2-禁用（用户状态）", campusUserTable.getDisable());
            map.put("1-未删除，2-删除（是否被删除）", campusUserTable.getDeleteType());
            map.put("创建时间", campusUserTable.getCreatTime());
            map.put("权限", campusUserTable.getPermission());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public CampusUserTable findUserByPhone(String phone) {
        CampusUserTable byPhone = campusUserTableRepository.findByPhone(phone);
        return byPhone;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePass(String username, String pass) {
        campusUserTableRepository.updatePass(username,pass);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatenickName(Long id, String name) {
        campusUserTableRepository.updateNick(id,name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePhone(Long id, String phone) {
        campusUserTableRepository.updatePhone(id,phone);
    }
}